package com.zhoug.widget.customview.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;


import com.zhoug.widget.R;
import com.zhoug.widget.adapter.BaseAdapter;

import java.util.List;

/**
 * 创建日期:2017/9/7 on 13:57
 * 描述：包含ListView的dialog
 * 作者: zhougan
 */

public class ListDialog extends Dialog {
    private final String TAG=ListDialog.class.getSimpleName();

    private List<String> data;
    private String title;
    private int width= WindowManager.LayoutParams.MATCH_PARENT;
    private int height= WindowManager.LayoutParams.WRAP_CONTENT;
    private int gravity= Gravity.BOTTOM;
    private AdapterView.OnItemClickListener onItemClickListener;
    private boolean quxiao=false;
    private int textGravity=Gravity.CENTER;

    private @StyleRes int windowAnimationResId=-1;


    public ListDialog(@NonNull Context context) {
        super(context);
    }

    /**
     *
     * @param context
     * @param themeResId R.style.dialog_full 左右无空隙
     */
    public ListDialog(@NonNull Context context, @StyleRes int themeResId) {
        super(context, themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//必须设置无标题 否则不会匹配内容
        setContentView(R.layout.list_dialog);

        ListView listView=findViewById(R.id.listView);
        View quxiaoGroup = findViewById(R.id.zg_list_cancel_group);
        TextView quxiaoBtn = findViewById(R.id.zg_list_cancel);

        //设置标题
        if(title!=null){
            TextView tvTitle=findViewById(R.id.textView1);
            LinearLayout llGroup=findViewById(R.id.linearLayout);
            if(llGroup!=null && tvTitle!=null){
                llGroup.setVisibility(View.VISIBLE);
                tvTitle.setText(title);
            }
        }

        if(quxiao && quxiaoGroup!=null && quxiaoBtn!=null){
            quxiaoGroup.setVisibility(View.VISIBLE);
            quxiaoBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(isShowing()){
                        dismiss();
                    }
                }
            });
        }else{
            if (quxiaoGroup != null) {
                quxiaoGroup.setVisibility(View.GONE);
            }
        }

        setListView(listView);

        setAttributes(width,height,gravity);

        if(windowAnimationResId!=-1){
            setWindowAnimations(windowAnimationResId);
        }

    }

    /**
     * 设置listView
     * @param listView
     */
    private void setListView(ListView listView){
        if(listView!=null){

            adapter.setData(data);
            listView.setAdapter(adapter);
            if(onItemClickListener!=null){
                listView.setOnItemClickListener(onItemClickListener);
            }

        }


    }

    private BaseAdapter<String> adapter=new BaseAdapter<String>() {
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View root=view;
            if(root==null){
                root=getLayoutInflater().inflate(R.layout.list_dialog_item,null);
            }
            TextView textView=root.findViewById(R.id.textView2);
            textView.setGravity(textGravity);
            String s= (String) getItem(i);
            textView.setText(s);

//            Log.i(TAG, "getView: s="+s);

            return root;
        }
    };


    public List<String> getData() {
        return data;
    }

    public ListDialog addQuxiaoBtn(boolean quxiao){
        this.quxiao=quxiao;
        return this;
    }

    public ListDialog setData(List<String> data) {
        this.data = data;
        return this;
    }

    public String getTitle() {
        return title;
    }

    public ListDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public ListDialog setTextGravity(int textGravity) {
        this.textGravity = textGravity;
        return this;
    }

    public ListDialog setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
        return this;
    }

    /**
     *设置窗口的属性
     * @param width 窗口宽 例：WindowManager.LayoutParams.MATCH_PARENT
     * @param height 高 例：200
     * @param gravity 位子 例：Gravity.BOTTOM
     */
    private ListDialog setAttributes(int width,int height,int gravity){
        WindowManager.LayoutParams attributes = this.getWindow().getAttributes();
        attributes.width=width;
        attributes.height= height;
        attributes.gravity= gravity;
        this.getWindow().setAttributes(attributes);
        return this;
    }

    public ListDialog setWidth(int width) {
        this.width = width;
        return this;
    }

    public ListDialog setHeight(int height) {
        this.height = height;
        return this;
    }

    public ListDialog setGravity(int gravity) {
        this.gravity = gravity;
        return this;
    }



    /**
     * 设置动画
     * @param resId  R.style.anim_window_popu动 画
     */
    public ListDialog setWindowAnimations(@StyleRes int resId){
        if ( this.getWindow()!=null){
            this.getWindow().setWindowAnimations(resId);
        }
        windowAnimationResId=resId;
        return this;
    }

    public ListDialog setDefWindowAnimations(){
        if ( this.getWindow()!=null){
            this.getWindow().setWindowAnimations(R.style.anim_window_popu);
        }
        windowAnimationResId=R.style.anim_window_popu;
        return this;
    }

}
